package com.hbisedm.interninfosys.intern.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbisedm.interninfosys.intern.entity.LastMenu;
import com.hbisedm.interninfosys.intern.entity.Menu;
import com.hbisedm.interninfosys.intern.entity.Meta;
import com.hbisedm.interninfosys.intern.mapper.MenuMapper;
import com.hbisedm.interninfosys.intern.service.IMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Sam
 * @since 2020-12-02
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    /**
     * 格式化菜单
     * @param menus
     * @return
     */
    public List<LastMenu> initMenu(List<Menu> menus) {
        ArrayList<LastMenu> lastMenus = new ArrayList<>();
        for (Menu menu : menus) {
            if(menu.getParentId() == null) {
                //定义子菜单List
                ArrayList<LastMenu> childMenus = new ArrayList<>();
                //定义菜单
                LastMenu lastMenu = new LastMenu();
                //复制属性
                BeanUtils.copyProperties(menu, lastMenu);
                //定义Meta
                Meta meta = new Meta();
                meta.setTitle(menu.getTitle());
                meta.setIcon(menu.getIcon());
                lastMenu.setMeta(meta);
                Integer menuId = menu.getMenuId();
                for (Menu menu1 : menus) {
                    if(menu1.getParentId()!=null) {
                        if(menu1.getParentId().equals(menuId)) {
                            LastMenu child = new LastMenu();
                            Meta childMeta = new Meta();
                            childMeta.setTitle(menu1.getTitle());
                            childMeta.setIcon(menu1.getIcon());
                            child.setMeta(childMeta);
                            BeanUtils.copyProperties(menu1, child);
                            childMenus.add(child);
                        }
                    }
                }
                lastMenu.setChildren(childMenus);
                lastMenus.add(lastMenu);
            }
        }
        System.out.println(lastMenus);

        return lastMenus;
    }
}
